package com.zbkj.service.service.monitor.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.aliyun.oss.OSSClientBuilder;
import com.zbkj.common.model.monitor.ZfbMonitorEquipmentHmyPictureNotice;
import com.zbkj.common.monitorchannel.vo.hemengyun.HeMengYunCallBackPictureNoticeVo;
import com.zbkj.common.monitorchannel.vo.hemengyun.HeMengYunResultVo;
import com.zbkj.common.vo.FileResultVo;
import com.zbkj.service.dao.monitor.ZfbMonitorEquipmentHmyPictureNoticeMapper;
import com.zbkj.service.service.UploadService;
import com.zbkj.service.service.monitor.IHeMengYunCallBackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * @ClassName: HeMengYunCallBackServiceImpl
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/9/21 11:00
 * @Version: V1.0
 **/
@Service
@Slf4j
public class HeMengYunPictureNoticeCallBackServiceImpl implements IHeMengYunCallBackService {


    @Autowired
    private ZfbMonitorEquipmentHmyPictureNoticeMapper zfbMonitorEquipmentHmyPictureNoticeMapper;

    @Autowired
    private UploadService uploadService;

    /**
     * PictureNotice 回调
     */
    @Override
    public void callBack(String action, String json) {
        if (!Objects.equals(action, "PictureNotice")) {
            return;
        }
        HeMengYunResultVo<HeMengYunCallBackPictureNoticeVo> resultVo = JSON.parseObject(json, new TypeReference<HeMengYunResultVo<HeMengYunCallBackPictureNoticeVo>>() {
        });
        HeMengYunCallBackPictureNoticeVo data = resultVo.getData();
        if (ObjectUtil.isNull(data)) {
            log.info("PictureNotice 回调数据为空={}", json);
            return;
        }

        String fileName = data.getLicense() + "_" + DateUtil.formatDateTime(new Date()) + ".jpg";

        MultipartFile multipart = this.convertUrlToMultipartFile(data.getUrl(), fileName);
        FileResultVo product = uploadService.imageUpload(multipart, "product", 1, null);


        ZfbMonitorEquipmentHmyPictureNotice pictureNotice = new ZfbMonitorEquipmentHmyPictureNotice();
        pictureNotice.setPath(data.getPath());
        pictureNotice.setUrl(data.getUrl());
        pictureNotice.setUploadUrl(product.getUrl());
        pictureNotice.setLicense(data.getLicense());
        pictureNotice.setStartTime(data.getStartTime());
        pictureNotice.setCreateTime(LocalDateTime.now());
        zfbMonitorEquipmentHmyPictureNoticeMapper.insert(pictureNotice);

    }


    private MultipartFile convertUrlToMultipartFile(String imageUrl, String fileName) {
        // 从URL获取输入流
        try (InputStream in = new URL(imageUrl).openStream()) {
            // 将输入流读取到字节数组
            byte[] bytes = IoUtil.readBytes(in);

            // 使用 MockMultipartFile 创建 MultipartFile 对象
            return new MockMultipartFile(fileName, fileName, "image/jpeg", bytes);
        } catch (Exception e) {
            log.error("图片转换异常", e);
        }
        return null;
    }
}
